// pages/fans/visitor.js
let axios = require("../../utils/util.js")
import Dialog from '../../miniprogram_npm/@vant/weapp/dialog/dialog';
Page({
  data: {
    swipeable: true,
    active: 1,
    page: 1,
    visitorList: [],
    myVisit: [],
    loading: false,
    visitorList_data: true, //是否有来访记录
    myVisit_data: true, //是否有足迹数据
    statusBarHeight:44,
  },
  onChange(event) {
    this.setData({
      page: 1,
      myVisit_data: true,
      visitorList_data: true,
      active:event.detail.index,
    })
    if (event.detail.index == 0) {
      this.visitorList()
    } else {
      this.myVisit()
    }
  },
  visitorList() {
    let params = {
      page: this.data.page
    };
    axios.axios({
      url: "/message/visitorList",
      data: params
    }).then((res) => {
      this.setData({
        visitorList: res.data.result,
      })
    })
  },
  myVisit() {
    let params = {
      page: this.data.page
    };
    axios.axios({
      url: "/user/myVisit",
      data: params
    }).then((res) => {
      this.setData({
        myVisit: res.data.result,
      })
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (options.fanstopNum == 2) {
      wx.setNavigationBarTitle({
        title: '最近来访', //页面标题
      })
      this.setData({
        active: 0
      })
    } else if (options.fanstopNum == 3) {
      wx.setNavigationBarTitle({
        title: '浏览足迹', //页面标题
      })
      this.setData({
        active: 1
      })
    }
    let that = this;
    wx.getSystemInfo({
      success: res => {
        that.setData({
          statusBarHeight: res.statusBarHeight,
        })
      },
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if (this.data.myVisit_data && this.data.active == 1) {
      this.data.page++
      this.setData({
        page: this.data.page,
        loading: true,
      })
      this.onReach_myVisit()
    } else if (this.data.active == 0 && this.data.visitorList_data) {
      this.data.page++
      this.setData({
        page: this.data.page,
        loading: true,
      })
      this.onReach_visitorList()
    }
  },
  //足迹
  onReach_myVisit() {
    let params = {
      page: this.data.page
    };
    axios.axios({
      url: "/user/myVisit",
      data: params
    }).then((res) => {
      if (res.data.result.length > 0) {
        setTimeout(() => {
          let a = this.data.myVisit;
          let b = res.data.result
          let c = a.concat(b);
          this.setData({
            myVisit_data: true,
            loading: false,
            myVisit: c,
          })
        }, 2000)
      } else {
        setTimeout(() => {
          this.setData({
            loading: false,
            myVisit_data: false,
          })
          wx.showToast({
            title: "没有更多",
            icon: 'none',
            duration: 2000 //持续的时间
          })
        }, 2000)
      }
    })
  },
  eliminate() {
    let that = this;
    Dialog.confirm({
      message: '确定清空所有浏览数据吗?',
      confirmButtonColor:'#0091FF',
      cancelButtonColor:'#999999',
    })
      .then(() => {
        // on confirm
        that.myVisitClear()
      })
      .catch(() => {
        // on cancel
      });
  },
  myVisitClear() {
    let params = {};
    axios.axios({
      url: "/user/myVisitClear",
      data: params
    }).then((res) => {
     if(res.data.status == 0){
       this.setData({
        myVisit:[]
       })
     }
     wx.showToast({
      title: res.data.text,
      icon: 'none',
      duration: 2000 //持续的时间
    })
    })
  },
  to_room(data) {
    let roomid = data.currentTarget.dataset.roomid
    wx.navigateTo({
      url: '/pages/room/room?roomId=' + roomid,
    })
  },
  fui_() {
    wx.navigateBack({ //返回
      delta: 1
    })
  },
  //个人主页
  tomemberId(data) {
    wx.navigateTo({
      url: '/pages/me/me?memberId=' + data.currentTarget.dataset.perfectnumber,
    })
  },
  //来访
  onReach_visitorList() {
    let params = {
      page: this.data.page
    };
    axios.axios({
      url: "/message/visitorList",
      data: params
    }).then((res) => {
      if (res.data.result.length > 0) {
        setTimeout(() => {
          let a = this.data.visitorList;
          let b = res.data.result
          let c = a.concat(b);
          this.setData({
            visitorList_data: true,
            loading: false,
            visitorList: c,
          })
        }, 2000)
      } else {
        setTimeout(() => {
          this.setData({
            loading: false,
            visitorList_data: false,
          })
        }, 2000)
      }
    })
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})
